<?php

namespace App\Orchid\Layouts\Tag;

use App\Models\Tag;
use Orchid\Screen\TD;
use Orchid\Screen\Actions\Link;
use Orchid\Screen\Layouts\Table;

class TagListLayout extends Table
{
    /**
     * Data source.
     *
     * The name of the key to fetch it from the query.
     * The results of which will be elements of the table.
     *
     * @var string
     */
    protected $target = 'tags';

    /**
     * Get the table cells to be displayed.
     *
     * @return TD[]
     */
    protected function columns(): iterable
    {
        return [
            TD::make('name', '标签名称')
                ->sort()
                ->filter(TD::FILTER_TEXT)
                ->render(function (Tag $tag) {
                    return Link::make($tag->name)
                        ->route('platform.tags.edit', $tag);
                }),
                
            TD::make('slug', 'Slug')
                ->sort(),
                
            TD::make('posts_count', '文章数量')
                ->render(function (Tag $tag) {
                    return $tag->posts()->count();
                }),
                
            TD::make('created_at', '创建时间')
                ->sort()
                ->render(function (Tag $tag) {
                    return $tag->created_at->format('Y-m-d H:i');
                }),
                
            TD::make('操作')
                ->align(TD::ALIGN_CENTER)
                ->width('150px')
                ->render(function (Tag $tag) {
                    return Link::make('编辑')
                        ->icon('pencil')
                        ->route('platform.tags.edit', $tag);
                }),
        ];
    }
}